/**
 * 
 */
package kr.co.insoft.publicmesh.controller.configration;

import kr.co.insoft.core.model.configration.vender.VenderEntityModel;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.service.configration.vender.VenderService;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import flexjson.JSONSerializer;

/**
 * @author skpark
 * 
 */
@Controller
@RequestMapping(value = "configration/vender")
public class VenderController {

	private static final Logger logger = LoggerFactory
			.getLogger(VenderController.class);

	final String TILES_PAGE_PREFIX = "tiles/configration/vender";
	final String SINGLE_PAGE_PREFIX = "configration/vender";

	@Autowired
	VenderService venderService;

	/**
	 * Vender 목록
	 * 
	 * @param model
	 * @param pagingModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "lists")
	public String lists(
			Model model,
			@ModelAttribute("pagingModel") PagingModel pagingModel,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {
		model.addAttribute("pagingModel", venderService.getLists(pagingModel));
		model.addAttribute("venderTypeModel", venderService.getVenderTypeList());

		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/lists";

		return TILES_PAGE_PREFIX + "/lists";
	}

	/**
	 * Vender 상세
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "view", method = RequestMethod.GET)
	public String view(
			Model model,
			@RequestParam("venderid") String venderid,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {

		model.addAttribute(venderService.getArticle(venderid));
		model.addAttribute("venderTypeList", venderService.getVenderTypeList());

		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/view";

		return TILES_PAGE_PREFIX + "/view";
	}

	/**
	 * Vender 등록화면
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.GET)
	public String createForm(
			Model model,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {
		model.addAttribute(new VenderEntityModel());
		model.addAttribute("venderTypeList", venderService.getVenderTypeList());
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/createForm";

		return TILES_PAGE_PREFIX + "/createForm";
	}

	/**
	 * Vender 등록
	 * 
	 * @param venderEntityModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.POST)
	public String createCloud(
			@ModelAttribute("venderEntityModel") VenderEntityModel venderEntityModel,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip,
			final BindingResult bindingResult) throws Exception {

		logger.info("{}",
				(new JSONSerializer()).deepSerialize(venderEntityModel));
		logger.info("{}", venderEntityModel.toString());

		if (bindingResult.hasErrors()) {
			if (ip.equals("Y"))
				return SINGLE_PAGE_PREFIX + "/createForm";

			return TILES_PAGE_PREFIX + "/createForm";
		}

		venderService.createVender(venderEntityModel);

		if (ip.equals("Y"))
			return "redirect:./lists?ip=Y";

		return "redirect:./lists";
	}

	/**
	 * Vender Type Sync
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "syncVender", method = RequestMethod.GET)
	public ResponseEntity<Boolean> syncVender() throws Exception {
		try {
			venderService.syncVenderType();
		} catch (Exception e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
		return new ResponseEntity<Boolean>(true, HttpStatus.OK);
	}

	/**
	 * Vender Name 중복 체크
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "isExistsVenderName", method = RequestMethod.GET)
	public ResponseEntity<Boolean> isExistsVenderName(
			@RequestParam(value = "venderName") String venderName)
			throws Exception {
		boolean isExist = false;
		try {
			int count = venderService.isExistsVenderName(venderName);
			if (count > 0)
				isExist = true;

		} catch (Exception e) {
			return new ResponseEntity<Boolean>(isExist, HttpStatus.OK);
		}
		return new ResponseEntity<Boolean>(isExist, HttpStatus.OK);
	}

	/**
	 * Vender 수정
	 * 
	 * @param venderEntityModel
	 * @return
	 */
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	public @ResponseBody
	ResponseEntity<Boolean> edit(
			@ModelAttribute("venderEntityModel") VenderEntityModel venderEntityModel) {
		try {
			venderService.editVender(venderEntityModel);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}

	/**
	 * Vender 삭제
	 * 
	 * @param venderEntityModel
	 * @return
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST)
	public @ResponseBody
	ResponseEntity<Boolean> remove(
			@ModelAttribute("venderEntityModel") VenderEntityModel venderEntityModel) {
		try {
			venderService.removeVender(venderEntityModel);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
}
